import $ from "jquery";

let start_x = 0;
let start_y = 0;

export function initialize(): void {
    $(document).on("mousedown", (e) => {
        start_x = e.pageX;
        start_y = e.pageY;
    });
}

export function is_drag(e: JQuery.ClickEvent): boolean {
    // Used to prevent click handlers from firing when dragging a
    // region, even if not actually selecting something.

    // It is possible to trigger mouse events programmatically
    // via EventTarget.dispatchEvent() or jQuery.trigger or Element.click.
    // Such events have the isTrusted property set to false.
    //
    // The isTrusted read-only property of the Event interface is a boolean
    // value that is true when the event was generated by the user agent
    // (including via user actions and programmatic methods such as
    // HTMLElement.focus()), and false when the event was dispatched via
    // EventTarget.dispatchEvent(). The only exception is the click event,
    // which initializes the isTrusted property to false in user agents.
    // https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
    //
    // We want to ignore programmatically triggered events as much as possible
    // to avoid them being misclassified as drags.
    if (!e.originalEvent?.isTrusted) {
        return false;
    }

    // Total distance the mouse has moved since the mouse went down.
    const drag_distance = Math.abs(e.pageX - start_x) + Math.abs(e.pageY - start_y);

    const sel = window.getSelection();
    const has_selection = sel?.type === "Range" && sel.toString().length > 0;

    // A very low drag_distance cutoff (2) can prevent a click after
    // moving the mouse rapidly from registering.
    //
    // So we only use that low cutoff when the drag resulted in
    // actually selecting something, and use a larger distance for
    // non-selection drags, like resizing textareas.
    return drag_distance > 20 || (drag_distance > 2 && has_selection);
}
